#include "stdafx.h"
#include "DropCounter.h"
#include "InstanceTouch.h"
#include <algorithm>

DECL_INSTANCE_TOUCH(ETouch_DropCounter);

struct  DropCounterReflectionRegister
{
	DropCounterReflectionRegister()
	{
		REGISTER_REFLECTION_MEMBER(DropCounter,m_iID,INVALID_ID).Editor_SetAsPrimaryKey();
		REGISTER_REFLECTION_MEMBER(DropCounter,m_sDisplayName,std::string("DefaultName")).Editor_SetAsDisplayName();
		REGISTER_REFLECTION_MEMBER(DropCounter,m_eCounter,eDropCounter0).Editor_SetAsEnumation<EDropCounter>();
		REGISTER_REFLECTION_MEMBER(DropCounter,m_nMatchNum,uint32(1)).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(DropCounter,m_iNextNum,INVALID_ID).Editor_SetAsRaw();
		REGISTER_REFLECTION_MEMBER(DropCounter,m_iFixedDropID,INVALID_ID).Editor_SetAsRaw();

		GlobalClassManager::Instance().RegisterClass(DropCounter::s_GetReflection(),eDefaultDef_DropCounter);
		GlobalDefManager::Instance().RegisterDefManger(&DropCounterManager::Instance());
	}
}__dropcounterreflectionregister;

int32 DropCounterManager::GetFixedDropID( EDropCounter eCounter,uint32 iCount) const
{
	if(eCounter<0||eCounter>=EDropCounter_NUM) 
		return INVALID_ID;

	const std::vector<const DropCounter*>& vDropCounter=m_vDropCounters[eCounter];
	for(uint32 ii=0;ii<vDropCounter.size();ii++)
	{
		const DropCounter* pDef=vDropCounter[ii];
		if(iCount<=pDef->m_nMatchNum)
		{
			return pDef->m_iFixedDropID;
		}
	}
	return INVALID_ID;
}


int32 DropCounterManager::GetNextCount( EDropCounter eCounter,uint32 iCount ) const
{
	if(eCounter<0||eCounter>=EDropCounter_NUM) 
		return 0;

	const std::vector<const DropCounter*>& vDropCounter=m_vDropCounters[eCounter];
	for(uint32 ii=0;ii<vDropCounter.size();ii++)
	{
		const DropCounter* pDef=vDropCounter[ii];
		if(iCount==pDef->m_nMatchNum)
		{
			return pDef->m_iNextNum;
		}
	}
	return INVALID_ID;
}
bool __CompareDropCountDefLess(const DropCounter*pA,const DropCounter*pB )
{
	return pA->m_nMatchNum<pB->m_nMatchNum;
}
rewrite  void DropCounterManager::OnArchieveReaded()
{
	for(uint32 ii=0;ii<EDropCounter_NUM;ii++)
	{
		m_vDropCounters[ii].clear();
	}

	std::vector<const DropCounter*> vEnum;
	Enumerate(vEnum);
	for(uint32 ii=0;ii<vEnum.size();ii++)
	{
		const DropCounter* pConter=vEnum[ii];
		if(pConter->m_eCounter<0||pConter->m_eCounter>=EDropCounter_NUM) continue;

		m_vDropCounters[pConter->m_eCounter].push_back(pConter);
	}
	for(uint32 ii=0;ii<EDropCounter_NUM;ii++)
	{
		std::sort(m_vDropCounters[ii].begin(),m_vDropCounters[ii].end(),__CompareDropCountDefLess);
	}
}
